package com.oa.control;

import com.oa.Response.ResponseData;
import com.oa.Response.Result;
import com.oa.entity.domain.User;
import com.oa.entity.dto.UserAndRole;
import com.oa.entity.vo.UserFormVo;
import com.oa.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags={"用户接口"})
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
     @Autowired
    private UserServiceImpl userService;

    @ApiOperation("新增用户接口")
    @PostMapping("/adduser")
    public User add(@RequestBody User user){
     return  user;
    }
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Map<String, String>> registy(@RequestBody UserFormVo userFormVo){
       return userService.resginter(userFormVo);
        //返回注册成功信息
    }
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseData<Map<String,String>> login(@RequestBody UserFormVo userFormVo){
//       User user = userService.getByNameAndPassword(userFormVo.getUsername(), userFormVo.getPassword());
        return  userService.login(userFormVo);
    }

    /**
     * 退出登录
     */
    public  ResponseData logout(){
        return userService.logout();
    }

    /**
     * 获取登录用户信息    返回角色和登录时间以及昵称
     * @return
     */
    @GetMapping("/getUserInfo")
    @PreAuthorize("@mser.hasAuthority('sys:index')")
    public  ResponseData<UserAndRole>  getUserInfo(){
        return userService.getUserInfo();
    }

    /**
     * 获取用户信息，包括密码，用户名，和id
     */
    @GetMapping("/getAllUser")
     public  ResponseData<List<User>> getAllUser(){
         return userService.getAllUser();
     }

    /**
     * 根据id更新用户名
     */
    @PostMapping("/updateUser")
     public  ResponseData  updateUser(@RequestBody User user){
        return  userService.updateUser( user.getId(),user.getName());
     }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @PostMapping("/deleteUser")
     public  ResponseData  deleteUser(@RequestBody User user){
        return userService.deleteUser(user.getId());
     }
    /**
     * 关键字查询
     */
    @GetMapping("/getUserBySomething/{msg}")
    public   ResponseData<List<User>> getUserBySomething(@PathVariable("msg") Object msg){
       return  userService.getUserBySomething(msg);
    }
    /**
     * 权限测试接口
     */
    @GetMapping("/hello")
//    @PreAuthorize("hasAuthority('sys:index')")
//    @PreAuthorize("@mser.hasAuthority('sys:index')")
    public  String hello(){
        return "hello";
    }
}
